﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SafeManage.PatrolPerson;
using LJX.SmartPark.Domain.SafeManage.PatrolRecord;
using LJX.SmartPark.Domain.SafeManage.Train;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.SafeManage
{
    public class TrainServeice:ITrainServeice
    {
        private readonly IRepository<Train> _trainService;

        public TrainServeice(IRepository<Train> trainService)
        {
            _trainService = trainService;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeleteTrainAsync(Train input)
        {
            var result = await _trainService.DeleteAsync(input.MapTo<Train>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _trainService.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fyTrain"></param>
        /// <returns></returns>
        public async Task<Response<List<Train>>> GetTrainAsync(FyTrain fyTrain)
        {
            var students = await _trainService.GetAllListAsync();
            var result = students.MapToList<Train>();

            if (!string.IsNullOrEmpty(fyTrain.tilite))
            {
                result = result.Where(a => a.Tilite.Contains(fyTrain.tilite)).ToList();
            }

            if (!string.IsNullOrEmpty(fyTrain.document))
            {
                result = result.Where(x => x.Document == int.Parse(fyTrain.document)).ToList();
            }

            var totalCount = result.Count();
            result = result.Skip(((fyTrain.pageIndex - 1) * fyTrain.pageSize)).Take(fyTrain.pageSize).ToList();
            return Response<List<Train>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<Train> GetTrainById(Train input)
        {
            var train = _trainService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = train.MapTo<Train>();
            return Response<Train>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddTrainAsync(Train input)
        {
            var result = _trainService.InsertAsyncById(input.MapTo<Train>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdTrainAsync(Train input)
        {
            var result = _trainService.UpdateAsync(input.MapTo<Train>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        public class FyTrain
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
            public string? tilite { get; set; }
            public string? document { get; set; }
        }
    }
}
